package activity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.laoxinwen.app.R;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Random;

import activity.main.MainActivity;
import bean.News;
import bean.Topic;
import bean.TopicNewsItem;
import bean.wraper.DefaultStringWrapper;
import common.Constants;
import common.FastAdapter;
import common.HttpCallBack;
import common.MyApp;
import common.MyTheme;
import common.Requester;
import common.ServerConfig;
import common.UserConfig;
import common.UserGuideUtil;
import common.VideoWebChromeClient;
import gallery.BasePicViewerActivity;
import gallery.PicViewData;
import share.LoginHelper;
import share.LoginType;
import share.ShareActivity;
import share.ShareDialog;
import util.LogicUtil;
import util.NetUtil;
import util.ToastUtil;
import view.SlidingMenu;

public class NewsActivity extends ShareActivity implements View.OnClickListener, LoginHelper.LoginResultListener, VideoWebChromeClient.VideoFullScreenHandler {
    //jscomment
    public static final String INTENT_KEY_NEWS = "currentNews";
    public static final String INTENT_KEY_NEWSGROUP = "newsgroup";
    private static final int TYPE_RELOAD = 1;
    private static final int TYPE_REFRESH_CMT_NUM = 2;
    private FastAdapter adapter;
    private Handler bottomBarHandler = new Handler();
    private News currentNews;
    @id(R.id.iv_loading)
    private ProgressBar progressBar;
    @id(R.id.listView)
    private ListView listView;
    @id(R.id.bt_care)
    private View vCollection;
    private Topic topic;
    private Dialog shareDialog;
    @id(R.id.slidingMenu)
    private SlidingMenu slidingMenu;
    @id(R.id.tv_topic_title)
    private TextView tvTopicTitle;
    private String url;
    @id(R.id.bottom_bar)
    private View vBottomBar;
    private PopupWindow fontManagePopup;
    private View modeNightImg;
    private View[] fontItems;
    private LoginHelper loginHelper;
    private Runnable showBottomBar = new Runnable() {
        @Override
        public void run() {
            AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
            alphaAnimation.setDuration(250);
            alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    vBottomBar.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            vBottomBar.startAnimation(alphaAnimation);
        }
    };

    @Override
    protected void onQqInfoObtained() {
        loginHelper.login(LoginType.QQ);
    }

    @Override
    protected void onWechatInfoObtain() {
        loginHelper.login(LoginType.WECHAT);
    }

    @Override
    protected void onWeiboInfoObtained() {
        loginHelper.login(LoginType.SINA);
    }

    @Override
    public void onLoginFail() {
        loginHelper.dismissDialog();
    }

    @Override
    public void onLoginSucceed() {
        loginHelper.dismissDialog();
    }

    private Runnable hideBottomBar = new Runnable() {
        @Override
        public void run() {
            AlphaAnimation alphaAnimation = new AlphaAnimation(vBottomBar.getAlpha(), 0);
            alphaAnimation.setDuration(250);
            alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    vBottomBar.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            vBottomBar.startAnimation(alphaAnimation);
        }
    };
    @id(R.id.webView)
    private WebView webView;
    private View.OnClickListener listenerForMenuNews = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TopicNewsItem n = (TopicNewsItem) v.getTag(2 << 24);
            currentNews = new News();
            currentNews.setId(n.getId());
            currentNews.setTid(topic.getId());
            currentNews.setTitle(n.getTitle());
            currentNews.setSummary(topic.getTitle());
            currentNews.setImg(n.getImg());
            url = LogicUtil.getNewsUrl(currentNews.getId(), false);
            slidingMenu.closeMenu();
            loadUrl();
            adapter.notifyDataSetChanged();
        }
    };
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case TYPE_RELOAD:
                    if (NetUtil.isNetworkConnected(NewsActivity.this)) {
                        loadUrl();
                    } else {
                        showToast(Constants.MSG_NET_ERROR);
                    }
                    break;
                case TYPE_REFRESH_CMT_NUM:
                    freshCmtNum();
                    break;
            }
        }
    };

    public static Intent getIntent(Context context, String newsId) {
        News news = new News();
        news.setId(newsId);

        return getIntent(context, news);
    }

    public static Intent getIntent(Context context, News news) {
        Intent it = new Intent(context, NewsActivity.class);
        it.putExtra(INTENT_KEY_NEWS, news);
        return it;
    }

    public static Intent getIntent(Context context, News news, Topic topic) {
        Intent it = new Intent(context, NewsActivity.class);
        it.putExtra(INTENT_KEY_NEWS, news);
        it.putExtra(INTENT_KEY_NEWSGROUP, topic);
        return it;
    }

    @Override
    public void onBackPressed() {
        if (slidingMenu.isMenuOpened()) {
            slidingMenu.closeMenu();
        } else if (webChromeClient.inCustomView()) {
            webChromeClient.onHideCustomView();
        } else if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
        fontManagePopup.dismiss();
    }

    private class Holder {
        View vRoot;
        TextView tvDate;
        TextView tvTitle;
        TextView tvYear;
        ImageView ivPic;
        View vLineUp;
        View vLineDown;
        View vYearDividerBlock;
        Calendar date = Calendar.getInstance();

        Holder(View v) {
            vRoot = v;
            tvDate = f(R.id.tv_date, v);
            tvTitle = f(R.id.tv_news_title, v);
            tvYear = f(R.id.tv_year, v);
            ivPic = f(R.id.iv_news_pic, v);
            vLineDown = f(R.id.line_down, v);
            vLineUp = f(R.id.line_up, v);
            vYearDividerBlock = f(R.id.year_divider_block, v);
        }

        void setData(TopicNewsItem news, int position) {
            vRoot.setTag(2 << 24, news);
            date.setTimeInMillis(news.getTime());
            int year = date.get(Calendar.YEAR);
            int month = date.get(Calendar.MONTH) + 1;
            int day = date.get(Calendar.DAY_OF_MONTH);

            tvTitle.setText(news.getTitle());
            tvDate.setText(String.format("%02d", month) + '/' + String.format("%02d", day));
            if ("1".equals(news.getKeynode())) {
                tvDate.setBackgroundColor(getResources().getColor(R.color.tl_datebg_keynode));
            } else {
                tvDate.setBackgroundColor(getResources().getColor(R.color.tl_datebg));

            }
            tvTitle.getPaint().setFakeBoldText(false);

            ivPic.setVisibility(View.GONE);

//            if (position == topic.getNews().size() - 1) {
//                ViewGroup.LayoutParams lp = vLineDown.getLayoutParams();
//                lp.height = 60;
//                vLineDown.setLayoutParams(lp);
//            } else {
//                vLineDown.setVisibility(View.VISIBLE);
//            }

            if (position == 0) {
                vYearDividerBlock.setVisibility(View.VISIBLE);
                tvYear.setText("" + year);
            } else {
                TopicNewsItem last = topic.getNews().get(position - 1);
                date.setTimeInMillis(last.getTime());
                if (date.get(Calendar.YEAR) != year) {
                    vYearDividerBlock.setVisibility(View.VISIBLE);
                    tvYear.setText("" + year);
                } else {
                    vYearDividerBlock.setVisibility(View.GONE);
                }
            }

            if (topic.getNews().get(position).getId().equals(currentNews.getId())) {
                tvTitle.setTextColor(Color.RED);
            } else {
                //标题颜色
                tvTitle.setTextColor(UserConfig.isReadedNews(news.getId()) ? MyApp.getMyTheme().getThemeColor(R.attr.font_minor_color) : MyApp.getMyTheme().getThemeColor(R.attr.font_main_color));
            }
        }


    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        loadView();
        setWebView();
        slidingMenu.disapbleSlide();
        // init popup
        View popview = getLayoutInflater().inflate(R.layout.font_setting_popup, null);
        fontManagePopup = new PopupWindow(popview, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        fontManagePopup.setTouchable(true);
        fontManagePopup.setOutsideTouchable(true);
        fontManagePopup.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        modeNightImg = popview.findViewById(R.id.mode_night_img);
        fontItems = new View[3];
        fontItems[0] = popview.findViewById(R.id.font_normal);
        fontItems[1] = popview.findViewById(R.id.font_big);
        fontItems[2] = popview.findViewById(R.id.font_large);

        topic = (Topic) getIntent().getSerializableExtra(INTENT_KEY_NEWSGROUP);

        currentNews = (News) getIntent().getSerializableExtra(INTENT_KEY_NEWS);
        setMenuBlock();
        url = LogicUtil.getNewsUrl(currentNews.getId(), null == topic);
        loginHelper = new LoginHelper(this);
        loginHelper.setLoginResultListener(this);
        loadUrl();
//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
//            f(R.id.v_shadow).setVisibility(View.GONE);
//        }

        //        bottomBarHandler.postDelayed(hideBottomBar, 2000);
        //        webView.loadUrl("http://www.baidu.com");
        ViewGroup rootView = (ViewGroup) f(R.id.detail_root);
        if (null == topic) {
            if(currentNews.ableWa()) {
                UserGuideUtil.checkGuide(UserGuideUtil.GUIDE_DETAIL_WA, rootView);
            }
        } else {
            UserGuideUtil.checkGuide(UserGuideUtil.GUIDE_DETAIL_CAT, rootView);
        }
    }

    @id(R.id.fullWebViewContainer)
    private FrameLayout fullWebViewContainer;

    private VideoWebChromeClient webChromeClient;

    @Override
    public void onFullScreen() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) slidingMenu.getLayoutParams();
        params.setMargins(0, 0, 0, 0);
        vBottomBar.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onExitFullScreen() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) slidingMenu.getLayoutParams();
        params.setMargins(0, (int) getResources().getDimension(R.dimen.abc_action_bar_default_height), 0, 0);
        vBottomBar.setVisibility(View.GONE);
    }

    @SuppressLint({"SetJavaScriptEnabled", "NewApi"})
    private void setWebView() {
        webChromeClient = new VideoWebChromeClient(webView, fullWebViewContainer, this);
        webView.setWebChromeClient(webChromeClient);
        webView.setWebViewClient(new WebViewClient() {


//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                try {
//                    Uri uri = Uri.parse(url);
//                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//                    startActivity(intent);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                return true;
//            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                showLoading();
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //记录readed
                UserConfig.setReadedNews(currentNews.getId());
                cancelLoading();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                if (!Constants.ERROR_HTML.equals(failingUrl)) {
                    loadErrorHtml();
                }
            }
        });
        WebSettings webSettings = webView.getSettings();
        webView.setVerticalScrollBarEnabled(true);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setBackgroundColor(Color.TRANSPARENT);
        webSettings.setUseWideViewPort(true);
        //		if(android.os.Build.VERSION.SDK_INT>10){
        //			webSettings.setDisplayZoomControls(false);//隐藏缩放按钮
        //		}
        //		webSettings.setBuiltInZoomControls(false);
//        webSettings.setLoadWithOverviewMode(true);
//        webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setJavaScriptEnabled(true);
    }

    private void setMenuBlock() {

        if (null == topic) {
            slidingMenu.disapbleSlide();
            f(R.id.bt_menu).setVisibility(View.GONE);
            f(R.id.bt_dig).setVisibility(currentNews.ableWa() ? View.VISIBLE : View.GONE);
        } else {
            f(R.id.bt_dig).setVisibility(View.GONE);
            f(R.id.bt_menu).setVisibility(View.VISIBLE);
            tvTopicTitle.setText(topic.getTitle());
            slidingMenu.setTrigerFromEdge(false);
            adapter = new FastAdapter() {


                @Override
                public int getCount() {
                    return topic.getNews().size();
                }


                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    TopicNewsItem newsItem = topic.getNews().get(position);
                    Holder holder = null;
                    if (null == convertView) {
                        convertView = LayoutInflater.from(NewsActivity.this).inflate(R.layout.topic_news_item, parent, false);
                        convertView.setClickable(true);
                        convertView.setOnClickListener(listenerForMenuNews);
                        holder = new Holder(convertView);
                        convertView.setTag(holder);
                    } else {
                        holder = (Holder) convertView.getTag();
                    }
                    holder.setData(newsItem, position);
                    return convertView;
                }

            };
            listView.setAdapter(adapter);
            for (int i = 0; i < topic.getNews().size(); i++) {
                if (topic.getNews().get(i).getId().equals(currentNews.getId())) {
                    listView.setSelection(i);
                    break;
                }
            }
        }

    }

    @SuppressLint("JavascriptInterface")
    private void loadUrl() {
        webView.loadUrl(url);
        webView.addJavascriptInterface(new Object() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @JavascriptInterface
            public void jscomment(String focus) {
                try {
                    JSONObject jsonObject = new JSONObject(focus);
                    if (jsonObject.getInt("focus") == 1) {
                        Intent it = CommentActivity.getIntent(NewsActivity.this, currentNews.getTid(), null, currentNews.getId(), null);
                        it.setAction(CommentActivity.ACTION_REQUEST_FOCUS);
                        startActivity(it);
                    } else {
                        Intent it = CommentActivity.getIntent(NewsActivity.this, currentNews.getTid(), null, currentNews.getId(), null);
                        startActivity(it);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @JavascriptInterface
            public void jsimgview(String content) {
                int i = 0;
                PicViewData data = com.alibaba.fastjson.JSONObject.parseObject(content, PicViewData.class);
                startActivity(BasePicViewerActivity.getIntent(data.getIndex(), data.getImgs(), NewsActivity.this));
            }

            @JavascriptInterface
            public void jsshare(String content) {
                try {
                    JSONObject jsonObject = new JSONObject(content);
                    switch (jsonObject.getString("platform")) {
                        case "pyq":
                            shareToWeixinFriendCircle(currentNews.getTitle(), currentNews.getSummary(), getImgForSharing(), getUrlForSharing());
                            break;
                        case "wx":
                            shareToWeixin(currentNews.getTitle(), currentNews.getSummary(), getImgForSharing(), getUrlForSharing());
                            break;
                        case "wb":
                            shareToWeibo(currentNews.getTitle(), currentNews.getSummary(), getImgForSharing(), getUrlForSharing());
                            break;
                    }
                    shareLog();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @JavascriptInterface
            public void jswa(String content) {
                //startActivity(DigTopicActivity.getIntentForDigNews(NewsActivity.this, currentNews.getId()));
                Intent it = TopicActivity.getIntent(NewsActivity.this, currentNews.getTid());
                startActivity(it);
            }

            @JavascriptInterface
            public void jsinfo(String content) {
                try {
                    News tmpNews = com.alibaba.fastjson.JSONObject.parseObject(content, News.class);
                    currentNews = tmpNews;
                    handler.obtainMessage(TYPE_REFRESH_CMT_NUM).sendToTarget();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "Android");
    }

    private void cancelLoading() {
        progressBar.setVisibility(View.GONE);
    }

    private void showLoading() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @SuppressLint("JavascriptInterface")
    private void loadErrorHtml() {
        webView.clearHistory();
        webView.loadUrl(Constants.ERROR_HTML);
        webView.addJavascriptInterface(new Object() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @JavascriptInterface
            public void jsreload(String content) {
                handler.sendEmptyMessage(TYPE_RELOAD);
            }
        }, "Android");
    }

    private String getImgForSharing() {
        return em(currentNews.getImg()) ? ServerConfig.DEFAULT_SHARE_IMG : currentNews.getImg();
    }

    private String getUrlForSharing() {
        String url = ServerConfig.SERVER_HOST + "/news/" + currentNews.getId();
        String userId = UserConfig.isLogined() ? UserConfig.getUserInfo().getId() : "0";
        Random r = new Random(System.currentTimeMillis());
        int random = r.nextInt(Integer.MAX_VALUE) + 1000;
        url += "?stamp=" + userId + "_" + System.currentTimeMillis() + random;
        return url;
    }

    private void shareLog() {
        Requester.shareLog("news", currentNews.getId(), new HttpCallBack<DefaultStringWrapper>() {
            @Override
            public void onSucceed(DefaultStringWrapper data) {

            }

            @Override
            protected void onNetError() {

            }

            @Override
            protected void onServerError(DefaultStringWrapper data) {

            }
        });
    }

    @Override
    protected void onRightSlide() {
        if (!slidingMenu.isMenuOpened()) {
            finish();
        }
        //        finish();
    }

    @Override
    protected void onLeftSlide() {

        if (!slidingMenu.isMenuOpened()) {
            startActivity(CommentActivity.getIntent(this, currentNews.getTid(), null, currentNews.getId(), null));
        }

    }

    @Override
    protected void onDownSlide() {
        if (vBottomBar.getVisibility() == View.GONE) {
            handler.post(showBottomBar);
        }
    }

    @Override
    protected void onUpSlide() {
        if (vBottomBar.getVisibility() == View.VISIBLE) {
            handler.post(hideBottomBar);
        }
    }

    @Override
    public void finish() {
        if (this.isTaskRoot()) {
            Intent it = new Intent(this, MainActivity.class);
            startActivity(it);
        }
        super.finish();
    }

    @Override
    public void onClick(View view) {
        hideInputKeyboard();
        switch (view.getId()) {
            case R.id.bt_menu:
                slidingMenu.openMenu();
                break;
            case R.id.bt_share:
                shareNews();
                break;
            case R.id.bt_dig:
                //startActivity(DigTopicActivity.getIntentForDigNews(NewsActivity.this, currentNews.getId()));
                Intent it = TopicActivity.getIntent(NewsActivity.this, currentNews.getTid());
                startActivity(it);
                break;
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_comment:
                startActivity(CommentActivity.getIntent(this, currentNews.getTid(), null, currentNews.getId(), null));
                break;
            case R.id.bt_font:
                fontManagePopup.showAtLocation(findViewById(R.id.detail_root), Gravity.BOTTOM, 0, 0);
                modeNightImg.setSelected(UserConfig.isNightMode());
                int cFontSize = UserConfig.getFontSize();
                for (View item : fontItems) {
                    if ((item.getId() == R.id.font_normal && cFontSize == Constants.FONT_SIZE_NORMAL) ||
                            (item.getId() == R.id.font_big && cFontSize == Constants.FONT_SIZE_BIG) ||
                            (item.getId() == R.id.font_large && cFontSize == Constants.FONT_SIZE_LARGE)) {
                        item.setSelected(true);
                    } else {
                        item.setSelected(false);
                    }
                }
                break;
            case R.id.bt_care:
                if (!UserConfig.isLogined()) {
                    showLoginDialog();
                } else {
                    Requester.markCareNews(currentNews.getId(), new HttpCallBack<DefaultStringWrapper>() {
                        @Override
                        public void onSucceed(DefaultStringWrapper data) {
                            //                        topic.setAttention(topic.getAttention() == 0 ? 1 : 0);
                            if (vCollection.isSelected()) {
                                ToastUtil.showShortToast("已取消关注");
                            } else {
                                ToastUtil.showShortToast("已关注");
                            }

                            vCollection.setSelected(!vCollection.isSelected());
                        }

                        @Override
                        protected void onServerError(DefaultStringWrapper data) {
                            super.onServerError(data);
                            MainActivity.jumpHereForLogin(NewsActivity.this);
                        }

                        @Override
                        protected void onNetError() {
                            super.onNetError();
                        }
                    });
                }
                break;
            case R.id.mode_night:
                UserConfig.setNightMode(!UserConfig.isNightMode());
                modeNightImg.setSelected(UserConfig.isNightMode());
                MyApp.switchTheme(UserConfig.isNightMode() ? MyTheme.THEME_NIGHT : UserConfig.getMainTheme());
                restartSelf();
                break;
            case R.id.font_normal:
            case R.id.font_big:
            case R.id.font_large:
                for (View item : fontItems) {
                    item.setSelected(false);
                }
                view.setSelected(true);
                UserConfig.setFontSize(view.getId() == R.id.font_normal ? Constants.FONT_SIZE_NORMAL : (view.getId() == R.id.font_big ? Constants.FONT_SIZE_BIG : Constants.FONT_SIZE_LARGE));
                url = LogicUtil.getNewsUrl(currentNews.getId(), null == topic);
                webView.clearHistory();
                loadUrl();
                break;
            case R.id.btn_complete:
                fontManagePopup.dismiss();
                break;
        }
    }

    private void freshCmtNum() {
        TextView tvNum = (TextView) findViewById(R.id.tv_cmt_num);
        if (currentNews.getComments_num() == 0) {
            tvNum.setVisibility(View.GONE);
        } else {
            tvNum.setVisibility(View.VISIBLE);
            if (currentNews.getComments_num() > 99) {
                tvNum.setText("99+");
            } else {
                tvNum.setText(currentNews.getComments_num() + "");
            }
        }
    }

    private void showLoginDialog() {
        loginHelper.showDialog();
    }

    private void shareNews() {
        shareDialog = ShareDialog.getShareDialog(this, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareDialog.dismiss();
                switch (v.getId()) {
                    case R.id.share_qq:
                        shareToQq(currentNews.getTitle(), currentNews.getSummary(), getImgForSharing(), getUrlForSharing());
                        break;
                    case R.id.share_qq_zone:
                        shareToQqZone(currentNews.getTitle(), currentNews.getSummary(), getImgForSharing(), getUrlForSharing());
                        break;
                    case R.id.share_weibo:
                        shareToWeibo(currentNews.getTitle(), currentNews.getSummary(), getImgForSharing(), getUrlForSharing());
                        break;
                    case R.id.share_weixin:
                        shareToWeixin(currentNews.getTitle(), currentNews.getSummary(), getImgForSharing(), getUrlForSharing());
                        break;
                    case R.id.share_weixin_friends:
                        shareToWeixinFriendCircle(currentNews.getTitle(), currentNews.getSummary(), getImgForSharing(), getUrlForSharing());
                        break;
                    case R.id.share_url:
                        shareCopyUrl(getUrlForSharing());
                        break;
                }
                shareLog();
            }
        });
        shareDialog.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("新闻页");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("新闻页");
    }
}

